package dev.kelvin.ecom.controller;

import dev.kelvin.ecom.model.Product;
import dev.kelvin.ecom.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/products")
    public ResponseEntity<List<Product>> productList() {
        return ResponseEntity.ok(productService.productList());
    }

    @GetMapping("/product/{id}")
    public ResponseEntity<Product> productById(@PathVariable int id) {
        Product product = productService.productById(id);
        if (null != product) {
            return ResponseEntity.ok(product);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/product")
    public ResponseEntity<?> addProduct(
            @RequestPart Product product,
            @RequestPart MultipartFile imageFile
    ) {
        try {
            Product product1 = productService.addProduct(product, imageFile);
            return ResponseEntity.ok(product1);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/product/{id}/image")
    public ResponseEntity<byte[]> getImageByProductId(@PathVariable int id) {
        Product product = productService.productById(id);
        byte[] imageData = product.getImageData();

        return ResponseEntity.ok()
                .contentType(MediaType.valueOf(
                        product.getImageType()
                )).body(imageData);
    }

    @PutMapping("/product/{id}")
    public ResponseEntity<String> updateProduct(
            @PathVariable int id,
            @RequestPart Product product,
            @RequestPart MultipartFile imageFile
    ) throws IOException {
        Product product1 = productService.updateProduct(id,product,imageFile);
        if (product1 != null){
           return ResponseEntity.ok("Updated");
        }else {
            return new ResponseEntity<>("Failed to update", HttpStatus.BAD_REQUEST);
        }
    }

    @DeleteMapping("/product/{id}")
    public ResponseEntity<String> removeProduct(@PathVariable int id){

        if (productService.productById(id) != null){
            productService.removeProduct(id);
            return ResponseEntity.ok("Deleted");
        } else{
            return ResponseEntity.ok("Product Not FouND");
        }
    }

    @GetMapping("/products/search")
    public ResponseEntity<List<Product>> searchProduct(@RequestBody String keyword){
        List<Product> productList = productService.searchProduct(keyword);
        return ResponseEntity.ok(productList);
    }


}


